package seismosurfer.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oracle.ord.im.OrdHttpUploadFile;
import oracle.ord.im.OrdHttpUploadFormData;
import seismosurfer.data.DocumentData;
import seismosurfer.domain.Document;
import seismosurfer.servlets.util.ServletUtil;
import seismosurfer.util.Registry;

/**
 * This servlet handles the document uploading and 
 * the url submission for an earthquake.
 *
 */
public class UploadServlet extends BaseServlet {
    
    private static final long serialVersionUID = 2259295250042516844L;

    protected static final String SUCCESS = "success";

    protected static final String ENCODING = "ISO-8859-7";

    // Initialize global variables
    public void init() throws ServletException {
        Registry.setJDBCURL(getServletContext().getInitParameter("jdbcURL"));
    }

    // Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //
        // Create an OrdHttpUploadFormData object and use it to parse the
        // multipart/form-data message.
        //
        OrdHttpUploadFormData formData = new OrdHttpUploadFormData(request);

        if (!formData.isUploadRequest()) {
            ServletUtil.displayUserError(response);
            return;
        }

        formData.enableParameterTranslation(ENCODING);

        formData.parseFormData();

        //
        // Get the quake id, description, info and document (file) or URL.
        //
        DocumentData dd = new DocumentData();

        long quakeid;
        try {
            quakeid = Integer.parseInt(formData.getParameter(QUAKEID));
        } catch (NumberFormatException e) {
            ServletUtil.displayUserError(response);
            return;
        }
        System.out.println("uploaded document quakeid: " + quakeid);
        dd.setQuakeID(quakeid);

        String description = formData.getParameter(DESCRIPTION);
        System.out.println("uploaded document description: " + description);
        dd.setDescription(description);

        String info = formData.getParameter(INFO);
        System.out.println("uploaded document info: " + info);
        dd.setInfo(info);

        OrdHttpUploadFile doc = null;

        String urlStr = formData.getParameter(DOCUMENT_URL);

        if (urlStr != null) {
            // A URL is being submitted...
            System.out.println("document url: " + urlStr);
            dd.setURL(urlStr);

            Document document = new Document(dd);
            boolean submitted = document.submitURL();

            if (submitted) {
                request.removeAttribute(ERROR);
                request.setAttribute(SUCCESS, "true");
            } else {
                setErrors(dd.getNotification(), request);
            }
            request.setAttribute(QUAKEID, new Long(quakeid));
            forward("/submitURL.jsp", request, response);
        } else {
            // A document is being uploaded...
            doc = formData.getFileParameter(DOCUMENT);

            dd.setDoc(doc);

            Document document = new Document(dd);
            boolean uploaded = document.uploadDocument();

            if (uploaded) {
                request.removeAttribute(ERROR);
                request.setAttribute(SUCCESS, "true");
            } else {
                setErrors(dd.getNotification(), request);
            }
            request.setAttribute(QUAKEID, new Long(quakeid));
            forward("/uploadDOC.jsp", request, response);
        }

        formData.release();
    }

    // Clean up resources
    public void destroy() {
    }
}
